package com.ruoyi.project.module.parameter.controller;

import java.util.List;

import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.parameter.domain.Parameter;
import com.ruoyi.project.module.parameter.service.IParameterService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 请求参数 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-10-08
 */
@Controller
@RequestMapping("/module/parameter")
public class ParameterController extends BaseController
{
    private String prefix = "module/parameter";
	
	@Autowired
	private IParameterService parameterService;

	@Autowired
	private IProjectService projectService;

	@RequiresPermissions("module:parameter:view")
	@GetMapping()
	public String parameter()
	{
	    return prefix + "/parameter";
	}
	
	/**
	 * 查询请求参数列表
	 */
	@RequiresPermissions("module:parameter:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Parameter parameter)
	{
		startPage();
        List<Parameter> list = parameterService.selectParameterList(parameter);
		return getDataTable(list);
	}
	
	/**
	 * 新增请求参数
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap )
	{
		Project info = projectService.selectProjectById(parentId);
		mmap.put("project", info);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存请求参数
	 */
	@RequiresPermissions("module:parameter:add")
	@Log(title = "请求参数", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Parameter parameter)
	{		
		return toAjax(parameterService.insertParameter(parameter));
	}

	/**
	 * 修改请求参数
	 */
	@GetMapping("/edit/{apiParameterId}")
	public String edit(@PathVariable("apiParameterId") Integer apiParameterId, ModelMap mmap)
	{
		Parameter parameter = parameterService.selectParameterById(apiParameterId);
		mmap.put("parameter", parameter);
		Project info = projectService.selectProjectById(parameter.getApiProjectnameId());
		mmap.put("project", info);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存请求参数
	 */
	@RequiresPermissions("module:parameter:edit")
	@Log(title = "请求参数", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Parameter parameter)
	{		
		return toAjax(parameterService.updateParameter(parameter));
	}
	
	/**
	 * 删除请求参数
	 */
	@RequiresPermissions("module:parameter:remove")
	@Log(title = "请求参数", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(parameterService.deleteParameterByIds(ids));
	}
	
}
